package com.xqzgj.system.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xqzgj.common.annotation.Log;
import com.xqzgj.common.core.controller.BaseController;
import com.xqzgj.common.core.domain.AjaxResult;
import com.xqzgj.common.enums.BusinessType;
import com.xqzgj.common.constant.UserConstants;
import com.xqzgj.system.domain.StockInfo;
import com.xqzgj.system.service.IStockInfoService;
import com.xqzgj.common.utils.poi.ExcelUtil;
import com.xqzgj.common.core.page.TableDataInfo;

/**
 * 库存信息Controller
 */
@RestController
@RequestMapping("/system/stockinfo")
public class StockInfoController extends BaseController {
    @Autowired
    private IStockInfoService stockInfoService;

    /**
     * 查询库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:stock:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockInfo stockInfo) {
        startPage();
        List<StockInfo> list = stockInfoService.selectStockInfoList(stockInfo);
        return getDataTable(list);
    }

    /**
     * 导出库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:stock:export')")
    @Log(title = "库存信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StockInfo stockInfo) {
        List<StockInfo> list = stockInfoService.selectStockInfoList(stockInfo);
        ExcelUtil<StockInfo> util = new ExcelUtil<StockInfo>(StockInfo.class);
        return util.exportExcel(list, "库存信息数据");
    }

    /**
     * 获取库存信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:stock:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(stockInfoService.selectStockInfoById(id));
    }

    /**
     * 新增库存信息
     */
    @PreAuthorize("@ss.hasPermi('system:stock:add')")
    @Log(title = "库存信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockInfo stockInfo) {
        if (!stockInfoService.checkGoodsCodeUnique(stockInfo)) {
            return error("新增商品'" + stockInfo.getGoodsCode() + "'失败，商品编码已存在");
        }
        if (!stockInfoService.checkGoodsNameUnique(stockInfo)) {
            return error("新增商品'" + stockInfo.getGoodsName() + "'失败，商品名称已存在");
        }
        return toAjax(stockInfoService.insertStockInfo(stockInfo));
    }

    /**
     * 修改库存信息
     */
    @PreAuthorize("@ss.hasPermi('system:stock:edit')")
    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockInfo stockInfo) {
        if (!stockInfoService.checkGoodsCodeUnique(stockInfo)) {
            return error("修改商品'" + stockInfo.getGoodsCode() + "'失败，商品编码已存在");
        }
        if (!stockInfoService.checkGoodsNameUnique(stockInfo)) {
            return error("修改商品'" + stockInfo.getGoodsName() + "'失败，商品名称已存在");
        }
        return toAjax(stockInfoService.updateStockInfo(stockInfo));
    }

    /**
     * 删除库存信息
     */
    @PreAuthorize("@ss.hasPermi('system:stock:remove')")
    @Log(title = "库存信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(stockInfoService.deleteStockInfoByIds(ids));
    }

    /**
     * 更新库存数量
     */
    @PreAuthorize("@ss.hasPermi('system:stock:edit')")
    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
    @PutMapping("/quantity")
    public AjaxResult updateQuantity(@RequestBody StockInfo stockInfo) {
        return toAjax(stockInfoService.updateStockQuantity(stockInfo));
    }

    /**
     * 更新库存状态
     */
    @PreAuthorize("@ss.hasPermi('system:stock:edit')")
    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult updateStatus(@RequestBody StockInfo stockInfo) {
        return toAjax(stockInfoService.updateStockStatus(stockInfo));
    }
}